package com.example.onlineeducation.utils


import androidx.recyclerview.widget.RecyclerView
import com.example.onlineeducation.adapter.PractiseProblemAdapter
import com.example.onlineeducation.adapter.ProblemAdapter
import com.example.onlineeducation.data.entity.ProblemEntity
import com.yc.jetpacklib.extension.showToast

/**
 * Creator: sindi
 * Date: 2022/4/27 16:42
 * UseDes:
 */
class PractiseProblemAdapterUtil {

    private lateinit var mRecyclerView: RecyclerView
    private val mAdapter: PractiseProblemAdapter = PractiseProblemAdapter()
    private var mCurrentPosition = 0
    var mProblemNumCall: ((currentPosition: Int, itemCount: Int) -> Unit)? = null
    var mSubmitSingleCall: ((currentPosition: Int, itemCount: Int) -> Unit)? = null//提交
    var mFinishCall: (() -> Unit)? = null//答题结束


    fun initRecyclerView(rv: RecyclerView) {
        mCurrentPosition = 0
        mRecyclerView = rv
        mRecyclerView.adapter = mAdapter
    }

    /*
    * 重新答题
    */
    fun revertOption() {
        mAdapter.revertOption(mCurrentPosition)
        mProblemNumCall?.invoke(mCurrentPosition, mAdapter.itemCount)
    }

    /*
    * 单题提交
    */
    fun submitSingle() {
        if (mAdapter.checkProblemSelect(mCurrentPosition)) {
            mSubmitSingleCall?.invoke(mCurrentPosition, mAdapter.itemCount)
        } else {
            mRecyclerView.context.showToast("请选择答案")
        }
    }




    /*
    * 下一题
    * */
    fun next() {
        val nextPosition = mCurrentPosition + 1
        if (nextPosition < mAdapter.itemCount) {
            if (mAdapter.checkProblemSelect(mCurrentPosition)) {
                mCurrentPosition = nextPosition
                mAdapter.next(mCurrentPosition)
                mRecyclerView.scrollToPosition(mCurrentPosition)
                mProblemNumCall?.invoke(mCurrentPosition, mAdapter.itemCount)
            } else {
                mRecyclerView.context.showToast("请选择答案")
            }
        } else {
            mFinishCall?.invoke()
        }
    }


    fun addAll(newData: List<ProblemEntity>?) {
        mAdapter.addAll(newData)
        mProblemNumCall?.invoke(mCurrentPosition, mAdapter.itemCount)
    }

    /*
    * 展示正确答案
    * */
    fun showCorrectAnswer(answerList: List<String>?) {
        mAdapter.showCorrectAnswer(mCurrentPosition, answerList)
    }

    fun getData() = mAdapter.getData()
}